package com.jdk.learn.juc2.thread.excrise;

import java.util.concurrent.*;

/**
 * 类功能描述:
 *    内存屏障
 * @author bgt
 * @version V1.0
 * @since 2019-02-27 10:01
 */
public class SemaphoreDemo {
    private static volatile Semaphore semaphore = new Semaphore(0);

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+" step  A");
                semaphore.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executorService.submit(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+" step  A");
                semaphore.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        semaphore.acquire(2);
        System.out.println("A------------");
        executorService.submit(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+" step  B");
                semaphore.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executorService.submit(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+" step  B");
                semaphore.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        semaphore.acquire(2);
        System.out.println("B------------");
        executorService.shutdown();
    }
}
